define(["jquery","comm/form/form.comm","comm/form/form.attention","comm/form/form.validate"],function($,$comm,$attention,$validate){
	var quest = {};

	/**
	 * 数据处理(若不需要数据处理，可以不写)
	 */
    quest.formatData = function(obj){
    	if( obj.list && $.isArray(obj.list) && obj.list.length > 0 ){
    		for(var i = 0; i < obj.list.length; i++){
    			if( typeof obj.list[i] == "string" ){
    				var str = obj.list[i];
    				obj.list[i] = {};
    				obj.list[i].key = str;
    				obj.list[i].text = str;
    			}
    		}
    	}
		return obj;
	}

	/**
	 * tpl模板
	 */
	quest.tpl = function(){
		return 	'<script id="tpl_question_radio" type="text/html">'+
				'	<div {{if relationId}}relationId="{{relationId}}"{{/if}} class="qaCard qaCard-radio form-group form-group-sm col-sm-{{length}} col-md-{{length}} grid-label-{{labelLength}}">'+
				'		<label class="control-label {{align}}">{{label}}</label>'+
				'		{{if status=="default"}}'+
				'   		<div class="control-input radio">'+
				'				{{each list as item i}}'+
				'					<label class="radio-inline">'+
				'						<input type="radio" class="validatebox" name="{{name}}" value="{{item.key}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" data-validate="{{validate}}" data-attention="{{attention}}">'+
				'						{{item.text}}'+
				'					</label>'+
				'				{{/each}}'+
				'   		</div>'+
				'		{{else if status=="readonly"}}'+	
				'   		<div class="control-input radio disabled">'+
				'				{{each list as item i}}'+
				'					<label class="radio-inline">'+
				'						<input type="radio" class="validatebox" name="{{name}}" value="{{item.key}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" data-validate="{{validate}}" data-attention="{{attention}}" disabled>'+
				'						{{item.text}}'+
				'					</label>'+
				'				{{/each}}'+
				'   		</div>'+
				'		{{else if status=="disabled"}}'+
				'   		<div class="control-input radio disabled">'+
				'				{{each list as item i}}'+
				'					<label class="radio-inline">'+
				'						<input type="radio" class="validatebox" name="{{name}}" value="{{item.key}}" data-type="{{type}}" data-quesId="{{id}}" data-innDeCode="{{innDeCode}}" data-deCode="{{deCode}}" data-dataUnit="{{dataUnit}}" data-validate="{{validate}}" data-attention="{{attention}}" disabled>'+
				'						{{item.text}}'+
				'					</label>'+
				'				{{/each}}'+
				'   		</div>'+
				'		{{/if}}'+
				'	</div>'+
				'</script>';
	}

	/**
	 * 赋值
	 */
	quest.setValue = function($dom,value){
		if( value ){
			$dom.each(function(i){
				if( value.quesValue == $(this).val() ){
					$(this).attr("checked","checked");
					return true; // 退出本次循环
				}
			});
		}
	}

	/**
	 * 获取value（保存时，获取每个dom对应的答案）
	 */
	quest.getValue = function($dom,name){
		var dom = $dom.parents(".qaCard").find("input[type='radio'][name='" + name + "']:checked");
		var value = {};
		if( dom.length > 0 ){
			value.quesValue = dom.val();
		}
		value.quesId = $dom.attr("data-quesId");
		value.deCode = $dom.attr("data-deCode");
		value.innDeCode = $dom.attr("data-innDeCode");
		var dataUnit = $dom.attr("data-dataUnit");
		if( dataUnit ) value.dataUnit = dataUnit;
		return value;
	}

	/**
	 * 事件函数
	 */
	quest.event = function(eventParams){
		$comm.cancelRadioChecked();
	}

	/**
	 * 提醒
	 */
	quest.attention = function(id){
        $("#" + id + " .qaCard-radio .validatebox").bind("blur",onAttention);
        // 初始化
        $("#" + id + " .qaCard-radio .validatebox").blur();
	}

	/**
	 * 校验
	 * flag:
	 * 	 true:  (点击保存按钮时使用)
	 * 	 false: 
	 */
	quest.validate = function(id,flag){
        $("#" + id + " .qaCard-radio .validatebox").bind("blur",onValidate);
        if( flag ){
        	var result = true;
        	$("#" + id + " .qaCard-radio").each(function(i){
        		var dom = $(this).find(".validatebox").eq(0);
        		var value = $(this).find("input[type='radio']:checked").length > 0 ? true : false;
        		var params = {
		            "value":value,
		            "dom":dom,
		            "flag":flag,
		        };
				var success = $validate.init(params,handleByRuleValidate,handleBytypeValidate);
				result = result && success;
	            if( !result ) return result;
        	});
        	return result;
        }
	}

	/**
	 * 获取输入的值(validate、attention中需要用到)
	 * @return boolean
	 */
	var getValue = function($dom){
		return $dom.parents(".qaCard").find("input[type='radio']:checked").context.checked;
	}

	/********************  attention begin  ********************/

	/**
	 * xx情况下触发的事件 (有可能是失去焦点等)
	 */
	var onAttention = function(){
		var params = {
            "value":getValue($(this)),
            "dom":$(this),
        };
		$attention.init(params,handleByRule,handleBytype);
    }

    /**
     * 根据单个规则rule获取单个规则的结果result
     * 该题型已有规则在case里拦截，公用规则从default里返回结果
     * @return Boolean [布尔值]
     */
    var handleByRule = function(obj){
    	var result;
        return result;
    }

    /**
     * 根据提醒类型(type)执行对应的方法
     * 该题型已有提醒类型在case里拦截，公用提醒类型在default里处理
     */
    var handleBytype = function(obj){
    	if( obj.type ){
    		switch(obj.type) {
    			case "tips_icon":    // 图标提醒(拦截重置)
	                tips_icon(obj);
	                break;
	            case "text_warn":    // 文字警告提醒(拦截过滤)
                    break;
	            default:
            		$attention.handleBytype(obj);
	        }
    	}
    }

    /**
     * 图标提示
     */
    var tips_icon = function(obj){
        view_tips_icon(obj)
    }

    /**
     * 图标提示
     */
    var view_tips_icon = function(obj){
        if( obj.result ){
            var $dom = obj.dom.parents(".control-input");
            if( !obj.view ) obj.view = {};
            if( !obj.view.icon ) obj.view.icon = "fa fa-question-circle";
            if( !obj.view.color ) obj.view.color = "#00E0BF";
            if( !obj.view.msg ) obj.view.msg = "提示";
            var h = '<i class="control-icon control-icon-2 '+ obj.view.icon +'" style="color:'+ obj.view.color +'" data-toggle="tooltip" data-placement="left" title="'+ obj.view.msg +'"></i>';
            if( $dom.find("i.control-icon").length == 0 ) $dom.append(h);
            $(function(){$('[data-toggle="tooltip"]').tooltip()});
        }
    }

    /********************  attention end  ********************/

    /********************  validate begin  ********************/

    /**
	 * xx情况下触发的事件 (有可能是失去焦点等)
	 */
	var onValidate = function(){
		var params = {
            "value":getValue($(this)),
            "dom":$(this),
        };
		$validate.init(params,handleByRuleValidate,handleBytypeValidate);
	}

	/**
     * 根据单个规则rule获取单个规则的结果result
     * 该题型已有规则在case里拦截，公用规则从default里返回结果
     * @return Boolean [布尔值]
     */
    var handleByRuleValidate = function(obj){
    	var result = true;
    	switch(obj.type) {
    		case "noNull":  // 非空
                result = noNull(obj);
                break;
        }
        return result;
    }

    /**
     * 根据提醒类型(type)执行对应的方法
     * 该题型已有提醒类型在case里拦截，公用提醒类型在default里处理
     */
    var handleBytypeValidate = function(obj){
    	if( obj.type ){
    		switch(obj.type) {
    			case "border_warn":    // 边框警告校验(拦截过滤)
                    break;
	            default:
            		$validate.handleBytype(obj);
	        }
    	}
    }

    /**
     * 校验规则 非空
     */
    var noNull = function(params){
        var success = params.value ? true : false;
        return success;
    }

	/********************  validate begin  ********************/
    
	return quest;
});